--谋善
local moushan = fk.CreateSkill {
    name = "mcdh_moushan",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

moushan:addEffect(fk.AfterCardsMove,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(self) then
                for _, move in ipairs(data) do
                    if move.from == player then
                        local n = 0
                        for _, info in ipairs(move.moveInfo) do
                            if info.fromArea == Card.PlayerHand then
                                n = n + 1
                            end
                        end
                        if n >= 2 then
                            return table.find(room:getAlivePlayers(), function(p) return not p:isKongcheng() end)
                        end
                    end
                end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets = room:getAlivePlayers()
        targets = table.filter(targets, function(p) return not p:isKongcheng() end)
        local players  = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#mcdh_moushan1-choice",
            skill_name = self.name,
          })
        if #players > 0 then
            local to = players[1]
            self.cost_data = to
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local card = room:askForCardChosen(player, self.cost_data, "h", self.name)
        room:throwCard(card, self.name, self.cost_data, player)
        if Fk:getCardById(card).trueName == "slash" then
            local p = room:askToChoosePlayers(player, {
                targets = room:getAlivePlayers(),
                min_num = 1,
                max_num = 1,
                prompt = "#mcdh_moushan2-choice",
                skill_name = self.name,
                cancelable = false
              })[1]
           p:drawCards(2, self.name)
        end
    end
})

moushan:addEffect(fk.Damaged,{
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        if player:hasSkill(self) and target == player then
                if data.from then
                    return table.find(room:getOtherPlayers(data.from), function(p) return not p:isKongcheng() end)
                else
                    return table.find(room:getAlivePlayers(), function(p) return not p:isKongcheng() end)
                end
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local targets
        if data.from then
            targets = room:getOtherPlayers(data.from)
        else
            targets = room:getAlivePlayers()
        end
        targets = table.filter(targets, function(p) return not p:isKongcheng() end)
        local players  = room:askToChoosePlayers(player, {
            targets = targets,
            min_num = 1,
            max_num = 1,
            prompt = "#mcdh_moushan1-choice",
            skill_name = self.name,
          })
        if #players > 0 then
            local to = players[1]
            self.cost_data = to
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local card = room:askForCardChosen(player, self.cost_data, "h", self.name)
        room:throwCard(card, self.name, self.cost_data, player)
        if Fk:getCardById(card).trueName == "slash" then
            local p = room:askToChoosePlayers(player, {
                targets = room:getAlivePlayers(),
                min_num = 1,
                max_num = 1,
                prompt = "#mcdh_moushan2-choice",
                skill_name = self.name,
                cancelable = false
              })[1]
           p:drawCards(2, self.name)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_moushan"] = "谋善",
    [":mcdh_moushan"] = "当你一次性失去至少两张手牌，或受到伤害后，你可以弃置伤害来源以外一名角色的一张手牌,若弃置牌为【杀】，你令一名角色摸两张牌。",
    ["#mcdh_moushan1-choice"] = "谋善:弃置伤害来源以外一名角色的一张手牌",
    ["#mcdh_moushan2-choice"] = "谋善:令一名角色摸两张牌",
}

return moushan